package clause

import "strings"

//实现结构体Clause拼接各个独立的子句
type Type int
type Clause struct {
	sql map[Type]string
	sqlVars map[Type][]interface{}
}
const(
	INSERT Type=iota
	VALUES
	SELECT
	LIMIT
	WHERE
	ORDERBY
	UPDATE
	DELETE
	COUNT
)
//根据Type调用对应的generator，生成该子句对应的SQL语句
func (c *Clause)Set(name Type,vars ...interface{}){
	if c.sql==nil{
		c.sql=make(map[Type]string)
		c.sqlVars=make(map[Type][]interface{})
	}
	sql,vars:=generators[name](vars...)
	c.sql[name]=sql
	c.sqlVars[name]=vars
}

//Build方法根据传入的Type的顺序，构造出最终的SQL语句
func (c *Clause)Build(orders ...Type)(string,[]interface{}){
	var sqls []string
	var vars []interface{}
	for _,order:=range orders{
		if sql,ok:=c.sql[order];ok{
			sqls=append(sqls,sql)
			vars=append(vars,c.sqlVars[order]...)
		}
	}
	return strings.Join(sqls," "),vars
}